import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
import type { UserType } from '../../types/socketType';

const initialState: UserType = {
  id: '',
  name: '',
  score: 0,
  hasAnswer: false,
};

const userSlice = createSlice({
  name: 'userSlice',
  initialState,
  reducers: {
    setUserName: (state, action: PayloadAction<string>) => {
      state.name = action.payload;
    },
    setUserId: (state, action: PayloadAction<string | null>) => {
      if (state.id === '') state.id = action.payload;
    },
    sethasAnswer: (state, action: PayloadAction) => {
      state.hasAnswer = true;
    },
    setFalseAnswer: (state, action: PayloadAction) => {
      state.hasAnswer = false;
    },
  },
});

export const { setUserName, setUserId, sethasAnswer, setFalseAnswer } = userSlice.actions;

export default userSlice.reducer;
